(* node.ml
 *
 * This file is part of camEngine
 * 
 * camEngine - A 3d motor for ocaml
 * Copyright (C) 2009 DAVY Guillaume <davyg2@gmail.com>
 *
 * camEngine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * camEngine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with camEngine.  If not, see <http://www.gnu.org/licenses/>.
 *)

open Vec3d;;
open Mesh;;
open Array;;
open Driver;;
open Matrice;;

type t=float
class node = object (this)
	val mutable pos = (new vec3d 0. 0. 0.)
	val mutable rot = (new vec3d 0. 0. 0.)
	val mutable sca = (new vec3d 0. 0. 0.)
	val mutable color = black
	val mutable transform = new matrice4
	
	method get_pos = pos;
	method get_rot = rot;
	method get_sca = sca;
	method get_color = color;
	
	method set_pos p = pos <- p;
	method set_rot r = rot <- r;
	method set_sca s = sca <- s;
	method set_color mycolor = color<-mycolor;
	
	method private updateTransform =
		transform#put 0 0 1.;
		transform#put 1 0 0.;
		transform#put 2 0 0.;
		transform#put 3 0 pos#get_x;
		transform#put 0 1 0.;
		transform#put 1 1 1.;
		transform#put 2 1 0.;
		transform#put 3 1 pos#get_y;
		transform#put 0 2 0.;
		transform#put 1 2 0.;
		transform#put 2 2 1.;
		transform#put 3 2 pos#get_z;
		transform#put 0 3 0.;
		transform#put 1 3 0.;
		transform#put 2 3 0.;
		transform#put 3 3 1.;
	
	method render (d:driver) =
		this#updateTransform;
		d#set_world transform;
end;;


class meshNode (m:mesh) = object (this)
	inherit node as myNode
	val mutable vertex = [| |]
	val mutable indices = []
	
	method get_mesh = m;
	
	(*method set_pos p =
		myNode#set_pos p;
		vertex <- [| |];
		for i = 0 to ((Array.length m#get_vertex) - 1) do
			let nt = Oo.copy m#get_vertex.(i) in
			nt#move pos;
			vertex <- (append [|nt|] vertex );
		done;
	
	method set_rot r =
		myNode#set_rot r;
	
	method set_sca s =
		myNode#set_sca s;*)
	
	method set_color myColor =
		myNode#set_color myColor;
		let rec aux = function
			| [] -> []
			| t::q -> let (t1,t2,t3) = t in (t1,t2,t3,color)::(aux q)
		in indices <- (aux m#get_indices);
	
	method render (d:driver) =
		myNode#render;
		d#drawFaces vertex indices;
	
	initializer
		this#set_pos (new vec3d 0. 0. 0.);
		this#set_color black;
	
end;;


